package com.tjbaobao.videoplayol.activity;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.TextView;

import com.tjbaobao.framework.ui.BaseTitleBar;
import com.tjbaobao.framework.utils.ConstantUtil;
import com.tjbaobao.framework.utils.DateTimeUtil;
import com.tjbaobao.framework.utils.FileUtil;
import com.tjbaobao.videoplayol.R;
import com.tjbaobao.videoplayol.base.AppActivity;
import com.tjbaobao.videoplayol.listener.OnVideoRecordListener;
import com.tjbaobao.videoplayol.modal.info.VideoCacheInfo;
import com.tjbaobao.videoplayol.ui.VideoRecordView;

import java.util.UUID;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 *
 * Created by TJbaobao on 2018/1/13.
 */

public class VideoRecordActivity extends AppActivity {

    @BindView(R.id.videoRecordView)
    VideoRecordView mVideoRecordView ;

    @BindView(R.id.ib_record)
    ImageButton ibRecord ;
    @BindView(R.id.tv_time)
    TextView tvTime ;

    private String videoPath ;
    private boolean isStart = false;

    private static int screenType = VideoRecordView.SCREEN_TYPE_HORIZONTAL ;
    private static int cameraType = VideoRecordView.CAMERA_TYPE_BACK ;
    @Override
    protected void onInitValues(Bundle bundle) {
        changeScreen();
        videoPath = ConstantUtil.getVideoFilesPath()+ UUID.randomUUID().toString()+".mp4";
    }

    @Override
    protected void onInitView() {
        requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉标题栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.video_record_activity_layout);
        ButterKnife.bind(this);
        mVideoRecordView.setScreenType(screenType);
        mVideoRecordView.setCameraType(cameraType);
        mVideoRecordView.setOnVideoRecordListener(new MyOnVideoRecordListener());
    }

    @Override
    protected void onInitTitleBar(BaseTitleBar baseTitleBar) {

    }

    @Override
    protected void onLoadData() {

    }

    /**
     * 强制切换横屏或者竖屏
     */
    private void changeScreen()
    {
        if(screenType==VideoRecordView.SCREEN_TYPE_HORIZONTAL)
        {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            getWindow().setFormat(PixelFormat.TRANSLUCENT);
        }
        else
        {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            getWindow().setFormat(PixelFormat.TRANSLUCENT);
        }
    }

    @OnClick({R.id.ib_record,R.id.ib_close,R.id.ib_change_camera,R.id.tv_change_screen})
    public void onVideoClick(View v)
    {
        switch (v.getId())
        {
            case R.id.ib_record:
                if(!isStart)
                {
                    isStart = mVideoRecordView.start(videoPath);
                    ibRecord.setImageResource(R.drawable.app_videoplay_view_bt_pause);
                }
                else
                {
                    mVideoRecordView.stop();
                    VideoCacheInfo info = new VideoCacheInfo();
                    info.setVideoPath(videoPath);
                    info.setVideoWidth(mVideoRecordView.getWidth());
                    info.setVideoHeight(mVideoRecordView.getHeight());
                    info.setVideoTime(mVideoRecordView.getVideoTime());
                    Intent intent = new Intent(this,VideoPreviewActivity.class);
                    intent.putExtra("videoCacheInfo",info);
                    startActivity(intent);
                    this.finish();
                }
                break;
            case R.id.ib_close:
                if(isStart)
                {
                    mVideoRecordView.stop();
                }
                FileUtil.delFileIfExists(videoPath);
                this.finish();
                break;
            case R.id.ib_change_camera:
               cameraType = mVideoRecordView.changeCamera();
                break;
            case R.id.tv_change_screen:
                screenType =   mVideoRecordView.changeScreen();
                changeScreen();
                break;
        }
    }

    private class MyOnVideoRecordListener implements OnVideoRecordListener
    {
        @Override
        public void onTime(long msec) {
            tvTime.setText(msecToStr(msec));
        }

        @Override
        public void onError(int errorCode, String errorMsg) {

        }

        private String msecToStr(long msec)
        {
            int s = (int) (msec/1000);
            int min = s/60;
            int sec = s%60;
            return DateTimeUtil.repair(min,2)+":"+DateTimeUtil.repair(sec,2);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mVideoRecordView.onDestroy();

    }
}
